<?php
namespace interface\v1;
/*
*来访登记接口类
*/

use verify;

class visitAppClass extends index
{
    private const page = 'visitor';
    private const table = 'visitor';
    private const title = '来访登记';
    /*选项*/
    public function search()
    {
        $this->code = 1;
        $data['result'] = [
            'idOption' => explode('，', para('visitorCertType')),
            'carNoOption' => explode('，', para('shortNameOption'))
        ];
        $this->resp['data'] = $data;
    }

    /**
     * 编辑
     */
    public function commitEdit(){
        $id = $this->post('id') ?: self::getId();
        $field = [
            'shortName' => $this->post('shortName'),
            'carNo' => $this->post('carNo'),
            'name' => $this->post('name'),
            'contactTel' => $this->post('contactTel'),
            'type' => $this->post('type'),
            'idNum' => $this->post('idNum'),
            'leaveTime' => $this->post('leaveTime'),
            'reason' => $this->post('reason'),
            'managerId' =>'',
        ];
        $field['visitDate'] = date('Y-m-d',strtotime($this->post('arrivalTime')));
        $field['arrivalTime'] = date('H:i:s',strtotime($this->post('arrivalTime')));
        $annotation = [
            'visitDate' => '日期',
            'name' => '姓名',
            'contactTel' => '联系方式',
            'arrivalTime' => '来访时间',
            'reason' => '来访事由',
        ];
        $rules = [
            'visitDate' => 'required',
            'name' => 'required',
            'contactTel' => 'required',
            'arrivalTime' => 'required',
            'reason' => 'required',
        ];
        // 校验是否符合规则
        $vt = new verify();
        $result = $vt->setAliases($annotation)->validate($field, $rules)->isOk();
        if ($result === false) {
            $this->resp['warn'] = $vt->getErrs()[0];  // 多个规则提示最前面一条
            goto resp;
        }
        //判断
        if(empty($this->post('arrivalTime'))){
            $this->resp['warn'] = '请选择来访时间';
        } elseif(self::total(array('table'=>self::table,'where'=>"contactTel ='$field[contactTel]' and visitDate = '".$this->date."'")) ==3){
            $this->resp['warn'] = '最多提交3条申请';
        }else {
            $where = " id = '$id' ";
            $info = query(self::table, $where);
            if (!$info['id']) {
                $userNo = self::generateCode(self::page,[
                    'prefix' => 'LFDJ',
                    'table' => self::table,
                    'field' => 'orderNo',
                    'where' => '',
                ],$field);
                $field = array_merge($field, [
                    'id' => $id,
                    'orderNo' => $userNo,
//                    'stid' => $this->control['stid'],
                ]);
                $json['warn'] = static::insert(["table" => self::table, "field" => $field]);
            } else {
                $json['warn'] = static::update([
                    "table" => self::table,
                    "field" => $field,
                    "where" => $where
                ]);
            }
            if (in_array($json['warn'], ["新增成功", "更新成功"])) {
                //添加日志
//                $type = mb_substr($json['warn'], 0, 2, 'utf-8');
//                $text = $this->control['name'] . $type . "了" . self::title . " id：【" . $id . '】';
//                $this->logText(["type" => self::page, "text" => $text]);
                //返回
                $this->code = 1;
            }
            $this->resp['warn'] = $json['warn'];
        }
        resp:
    }
}